package com.movie.controller.SysCommentController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.movie.beans.Comment;
import com.movie.beans.Const.Const;
import com.movie.beans.SiteRate;
import com.movie.beans.SysUser;
import com.movie.beans.base.AjaxResult;
import com.movie.beans.dto.CommentDto;
import com.movie.service.CommentService;
import com.movie.service.MovieService;
import com.movie.service.SiteRateService;
import com.movie.service.UserService;
import com.movie.utils.DateUtils;
import com.movie.utils.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/comment")
public class SysCommentController {
    private static String prefix = "/system/comment";
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;
    @Autowired
    private SiteRateService siteRateService;
    @Autowired
    private MovieService movieService;

    @GetMapping("/{id}")
    public String index(@PathVariable(value = "id")Long id,int limit,int page, ModelMap modelMap) {
        Comment c = new Comment();
        c.setMovieId(id);
        PageHelper.startPage(page,limit);
        List<Comment> comments = commentService.selectCommentById(c);
        List<CommentDto> commentDtos = new ArrayList<>();
        for (Comment item:comments) {
            CommentDto commentDto = new CommentDto();
            commentDto.setId(item.getId());
            commentDto.setTime(item.getTime());
            commentDto.setUserId(item.getUserId());
            commentDto.setStatus(item.getStatus());
            commentDto.setMovieId(item.getMovieId());
            commentDto.setComment(item.getComment());
            commentDto.setUserName(userService.selectUserById(item.getUserId()).getUserName());
            commentDto.setLoginName(userService.selectUserById(item.getUserId()).getLoginName());
            SiteRate siteRate1 = siteRateService.selectSiteRateByMovieId(item.getMovieId());
            if (siteRate1!=null){
                commentDto.setRate((int) (siteRate1.getRate()*10));
            }
            else {
                commentDto.setRate((int)(0.0*10));
            }
            commentDtos.add(commentDto);
        }
        modelMap.addAttribute("title",movieService.getMoviesById(id).getTitle());
        modelMap.addAttribute("comments",commentDtos);
        modelMap.addAttribute("curr",page);
        modelMap.addAttribute("movieId",id);
        modelMap.addAttribute("count",new PageInfo(comments).getTotal());
        return "/page/comment";
    }

    @GetMapping()
    public String index(ModelMap modelMap){
        SysUser user = ShiroUtil.getSysUser();
        if(user.isAdmin()){
            modelMap.addAttribute("style","");
        }
        else{
            modelMap.addAttribute("style","none");
        }
        return prefix+"/index";
    }
    @PostMapping("/insert")
    @ResponseBody
    public AjaxResult insert(Comment comment){
        SysUser user = ShiroUtil.getSysUser();
        Comment c2 = new Comment();
        c2.setUserId(user.getUserId());
        c2.setMovieId(comment.getMovieId());
        List<Comment> c = commentService.selectCommentById(c2);
        if(c.size()!=0){
            return AjaxResult.warn("您以对该电影评论过了，无法重复评论");
        }
        comment.setStatus(Const.STATUS_NORMAL);
        comment.setTime(DateUtils.getNowDate());
        comment.setUserId(user.getUserId());
        int i = commentService.insert(comment);
        if(i>0){
            return AjaxResult.success("评论成功");
        }
        return AjaxResult.error("评论失败");
    }

    @PostMapping("/list")
    @ResponseBody
    public Map list(Comment comment,int limit,int page){
        SysUser user = ShiroUtil.getSysUser();
        Map map = new HashMap();
        if (user.isAdmin()){
            System.out.println(comment);
            PageHelper.startPage(page,limit);
            List<Comment> c = commentService.selectCommentById(comment);
            List<CommentDto> commentDtos = new ArrayList<>();
            for (Comment item:c) {
                CommentDto commentDto = new CommentDto();
                commentDto.setTitle(movieService.getMoviesById(item.getMovieId()).getTitle());
                commentDto.setId(item.getId());
                commentDto.setTime(item.getTime());
                commentDto.setUserId(item.getUserId());
                commentDto.setStatus(item.getStatus());
                commentDto.setMovieId(item.getMovieId());
                commentDto.setComment(item.getComment());
                commentDto.setUserName(userService.selectUserById(item.getUserId()).getUserName());
                commentDto.setLoginName(userService.selectUserById(item.getUserId()).getLoginName());
                commentDtos.add(commentDto);
            }
            map.put("msg","");
            map.put("code",0);
            map.put("data",commentDtos);
            map.put("count",new PageInfo(c).getTotal());
            return map;
        }
        else {
            comment.setUserId(user.getUserId());
            PageHelper.startPage(page,limit);
            List<Comment> c = commentService.selectCommentById(comment);
            List<CommentDto> commentDtos = new ArrayList<>();
            for (Comment item:c) {
                CommentDto commentDto = new CommentDto();
                commentDto.setId(item.getId());
                commentDto.setTime(item.getTime());
                commentDto.setUserId(item.getUserId());
                commentDto.setStatus(item.getStatus());
                commentDto.setMovieId(item.getMovieId());
                commentDto.setComment(item.getComment());
                commentDto.setUserName(userService.selectUserById(item.getUserId()).getUserName());
                commentDto.setLoginName(userService.selectUserById(item.getUserId()).getLoginName());
                commentDtos.add(commentDto);
            }
            map.put("msg","");
            map.put("code",0);
            map.put("data",commentDtos);
            map.put("count",new PageInfo(c).getTotal());
            return map;
        }

    }

    @PostMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable(value = "id")Long id){
        if (commentService.remove(id) > 0)
        {
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除异常，请联系管理员");
    }
}
